package dns

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"fmt"
	"testing"

	ibclient "github.com/infobloxopen/infoblox-go-client/v2"

	"github.com/k8gb-io/k8gb/controllers/mocks"
	"github.com/k8gb-io/k8gb/controllers/resolver"
	"github.com/stretchr/testify/assert"
	"go.uber.org/mock/gomock"
)

func TestCreateZoneDelegationInfoblox(t *testing.T) {

	tests := []struct {
		name          string
		config        resolver.Config
		getClient     func(*gomock.Controller) InfobloxClient
		expectedError bool
	}{
		{
			name:          "create cloud.example.com",
			expectedError: false,
			config: resolver.Config{
				K8gbNamespace: "k8gb",
				NSRecordTTL:   30,
				DelegationZones: []*resolver.DelegationZoneInfo{
					{
						ParentZone:       "example.com",
						LoadBalancedZone: "cloud.example.com",
						ClusterNSName:    "gslb-ns-eu-cloud.example.com",
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						NegativeTTL:      30,
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-cloud.example.com",
						},
					},
				},
			},
			getClient: func(ctrl *gomock.Controller) InfobloxClient {
				findZone := &ibclient.ZoneDelegated{
					DelegateTo: ibclient.NullableNameServers{
						NameServers: []ibclient.NameServer{
							{
								Name:    "gslb-ns-eu-cloud.example.com",
								Address: "10.0.0.1",
							},
							{
								Name:    "gslb-ns-us-cloud.example.com",
								Address: "10.0.0.2",
							},
						},
						IsNull: false,
					},
				}

				mgr := mocks.NewMockIBObjectManager(ctrl)
				client := mocks.NewMockInfobloxClient(ctrl)
				mgr.EXPECT().GetZoneDelegated("cloud.example.com").Return(nil, nil).Times(1)
				mgr.EXPECT().CreateZoneDelegated(gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any()).
					Return(findZone, nil).Times(1)
				client.EXPECT().GetObjectManager().Return(mgr, nil).AnyTimes()
				return client
			},
		},

		{
			name:          "found cloud.example.com WITHOUT updating",
			expectedError: false,
			config: resolver.Config{
				K8gbNamespace: "k8gb",
				NSRecordTTL:   30,
				DelegationZones: []*resolver.DelegationZoneInfo{
					{
						ParentZone:       "example.com",
						LoadBalancedZone: "cloud.example.com",
						ClusterNSName:    "gslb-ns-eu-cloud.example.com",
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						NegativeTTL:      30,
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-cloud.example.com",
							"za": "gslb-ns-za-cloud.example.com",
						},
					},
				},
			},
			getClient: func(ctrl *gomock.Controller) InfobloxClient {
				findZone := &ibclient.ZoneDelegated{
					DelegateTo: ibclient.NullableNameServers{
						NameServers: []ibclient.NameServer{
							{
								Name:    "gslb-ns-eu-cloud.example.com",
								Address: "10.0.0.1",
							},
							{
								Name:    "gslb-ns-us-cloud.example.com",
								Address: "10.0.0.2",
							},
						},
						IsNull: false,
					},
				}

				mgr := mocks.NewMockIBObjectManager(ctrl)
				client := mocks.NewMockInfobloxClient(ctrl)
				mgr.EXPECT().GetZoneDelegated("cloud.example.com").Return(findZone, nil).AnyTimes()
				client.EXPECT().GetObjectManager().Return(mgr, nil).AnyTimes()
				return client
			},
		},

		{
			name:          "found cloud.example.com WITH updating",
			expectedError: false,
			config: resolver.Config{
				K8gbNamespace: "k8gb",
				NSRecordTTL:   30,
				DelegationZones: []*resolver.DelegationZoneInfo{
					{
						ParentZone:       "example.com",
						LoadBalancedZone: "cloud.example.com",
						ClusterNSName:    "gslb-ns-eu-cloud.example.com",
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						NegativeTTL:      30,
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-cloud.example.com",
							"za": "gslb-ns-za-cloud.example.com",
						},
					},
				},
			},
			getClient: func(ctrl *gomock.Controller) InfobloxClient {
				findZone := &ibclient.ZoneDelegated{
					Ref: "cloud.example.com",
					DelegateTo: ibclient.NullableNameServers{
						NameServers: []ibclient.NameServer{
							{
								Name:    "gslb-ns-eu-cloud.example.com",
								Address: "10.0.0.1",
							},
							{
								Name:    "gslb-ns-us-cloud.example.com",
								Address: "10.0.0.2",
							},
						},
						IsNull: false,
					},
				}

				nsAfterSanitazion := ibclient.NullableNameServers{
					NameServers: []ibclient.NameServer{
						{
							Name:    "gslb-ns-eu-cloud.example.com",
							Address: "10.0.0.1",
						},
						{
							Name:    "gslb-ns-eu-cloud.example.com",
							Address: "10.0.0.2",
						},
						{
							Name:    "gslb-ns-us-cloud.example.com",
							Address: "10.0.0.2",
						},
					},
					IsNull: false,
				}

				mgr := mocks.NewMockIBObjectManager(ctrl)
				client := mocks.NewMockInfobloxClient(ctrl)
				mgr.EXPECT().GetZoneDelegated("cloud.example.com").Return(findZone, nil).Times(1)
				mgr.EXPECT().UpdateZoneDelegated(
					findZone.Ref,
					nsAfterSanitazion,
					"updated by k8gb",
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					true,
					ibclient.EA{}).
					Return(findZone, nil).Times(1)
				client.EXPECT().GetObjectManager().Return(mgr, nil).AnyTimes()
				return client
			},
		},
		{
			name:          "error during Infoblox interraction",
			expectedError: true,
			config: resolver.Config{
				K8gbNamespace: "k8gb",
				NSRecordTTL:   30,
				DelegationZones: []*resolver.DelegationZoneInfo{
					{
						ParentZone:       "example.com",
						LoadBalancedZone: "cloud.example.com",
						ClusterNSName:    "gslb-ns-eu-cloud.example.com",
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						NegativeTTL:      30,
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-cloud.example.com",
							"za": "gslb-ns-za-cloud.example.com",
						},
					},
				},
			},
			getClient: func(ctrl *gomock.Controller) InfobloxClient {
				mgr := mocks.NewMockIBObjectManager(ctrl)
				client := mocks.NewMockInfobloxClient(ctrl)
				mgr.EXPECT().GetZoneDelegated("cloud.example.com").Return(nil, fmt.Errorf("error")).Times(1)
				mgr.EXPECT().CreateZoneDelegated(gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any()).
					Return(nil, fmt.Errorf("error")).AnyTimes()
				client.EXPECT().GetObjectManager().Return(mgr, nil).AnyTimes()
				return client
			},
		},
		{
			name:          "create cloud.example.com with custom DNS view",
			expectedError: false,
			config: resolver.Config{
				K8gbNamespace: "k8gb",
				NSRecordTTL:   30,
				Infoblox: resolver.Infoblox{
					DNSView: "custom-view",
				},
				DelegationZones: []*resolver.DelegationZoneInfo{
					{
						ParentZone:       "example.com",
						LoadBalancedZone: "cloud.example.com",
						ClusterNSName:    "gslb-ns-eu-cloud.example.com",
						IPs:              []string{"10.0.0.1", "10.0.0.2"},
						NegativeTTL:      30,
						ExtClusterNSNames: map[string]string{
							"us": "gslb-ns-us-cloud.example.com",
						},
					},
				},
			},
			getClient: func(ctrl *gomock.Controller) InfobloxClient {
				findZone := &ibclient.ZoneDelegated{
					DelegateTo: ibclient.NullableNameServers{
						NameServers: []ibclient.NameServer{
							{
								Name:    "gslb-ns-eu-cloud.example.com",
								Address: "10.0.0.1",
							},
							{
								Name:    "gslb-ns-us-cloud.example.com",
								Address: "10.0.0.2",
							},
						},
						IsNull: false,
					},
				}

				mgr := mocks.NewMockIBObjectManager(ctrl)
				client := mocks.NewMockInfobloxClient(ctrl)
				mgr.EXPECT().GetZoneDelegated("cloud.example.com").Return(nil, nil).Times(1)
				mgr.EXPECT().CreateZoneDelegated(gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					gomock.Any(),
					"custom-view",
					gomock.Any()).
					Return(findZone, nil).Times(1)
				client.EXPECT().GetObjectManager().Return(mgr, nil).AnyTimes()
				return client
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			ctrl := gomock.NewController(t)
			defer ctrl.Finish()
			client := test.getClient(ctrl)
			ibx := NewInfobloxDNS(test.config, client)
			for _, zone := range test.config.DelegationZones {
				err := ibx.CreateZoneDelegation(zone)
				if test.expectedError {
					assert.Error(t, err)
					continue
				}
				assert.NoError(t, err)
			}
		})
	}
}
